package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.Consumer;
import com.ruoyi.system.domain.Vehicle;
import com.ruoyi.system.domain.dto.ConsumerDto;
import com.ruoyi.system.domain.vo.ConsumerVo;
import com.ruoyi.system.domain.vo.VehicleVo;
import com.ruoyi.system.service.ConsumerService;
import com.ruoyi.system.mapper.ConsumerMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 22115
* @description 针对表【consumer】的数据库操作Service实现
* @createDate 2025-02-14 09:41:48
*/
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer>
    implements ConsumerService{

    @Override
    public AjaxResult ConsumerList(ConsumerDto consumerDto) {
        Page<Consumer> consumerPage = new Page<>(consumerDto.getPageNum(), consumerDto.getPageSize());

        Page<ConsumerVo> consumerVoPage = new Page<>(consumerDto.getPageNum(), consumerDto.getPageSize());

        LambdaQueryWrapper<Consumer> consumerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        consumerLambdaQueryWrapper.like(StringUtils.isNotNull(consumerDto.getNickName()),Consumer::getNickName, consumerDto.getNickName());
        consumerLambdaQueryWrapper.eq(StringUtils.isNotNull(consumerDto.getStatus()), Consumer::getStatus, consumerDto.getStatus());

        Page<Consumer> page = this.page(consumerPage, consumerLambdaQueryWrapper);

        List<ConsumerVo> consumerVos = new ArrayList<>();
        List<Consumer> records = page.getRecords();
        for (int i = 0; i < records.size(); i++) {
            ConsumerVo consumerVo = new ConsumerVo();

            BeanUtils.copyBeanProp(consumerVo,records.get(i));
            consumerVo.setPassword("");
            consumerVos.add(consumerVo);
        }

        consumerVoPage.setRecords(consumerVos);
        return AjaxResult.success(consumerVoPage);
    }

    @Override
    public AjaxResult updateConsumer(ConsumerDto consumerDto) {
        Consumer consumer = new Consumer();
        BeanUtils.copyBeanProp(consumer,consumerDto);
        boolean b = this.updateById(consumer);
        if (b){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    public AjaxResult addConsumer(ConsumerDto consumerDto) {
        Consumer consumer = new Consumer();
       BeanUtils.copyBeanProp(consumer,consumerDto);
        boolean save = this.save(consumer);

        if (save){
            return AjaxResult.success();
        }

        return AjaxResult.error();
    }

    @Override
    public AjaxResult detConsumer(ConsumerDto consumerDto) {
        Consumer consumer = new Consumer();
        BeanUtils.copyBeanProp(consumer,consumerDto);
        boolean b = this.removeById(consumer);
        if (b){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}




